﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Kinect;
using System.IO;

namespace KinectStudio.ModuliAudio
{
    /// <summary>
    /// Interaction logic for Sonar.xaml
    /// </summary>
    public partial class Sonar : Window
    {

        private KinectSensor sensore;

        public Sonar()
        {
            InitializeComponent();
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuCliccato = (MenuItem)e.OriginalSource;
            switch (menuCliccato.Header.ToString())
            {
                case "Esci":
                    this.Close();
                    break;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (KinectSensor possibileSensore in KinectSensor.KinectSensors)
            {
                if (possibileSensore.Status == KinectStatus.Connected)
                {
                    this.sensore = possibileSensore;
                    break;
                }
            }

            if (this.sensore != null)
            {
                try
                {
                    this.sensore.Start();
                }
                catch (IOException)
                {
                    this.sensore = null;
                }
            }
            if (this.sensore == null)
            {
                MessageBox.Show("Nessun Kinect Collegato!", "Errore", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            this.sensore.AudioSource.BeamAngleChanged += this.AudioSourceBeamChanged;
            this.sensore.AudioSource.SoundSourceAngleChanged += this.AudioSourceSoundSourceAngleChanged; 
            this.sensore.AudioSource.Start();

        }

        private void AudioSourceBeamChanged(object sender, BeamAngleChangedEventArgs e)
        {
            this.ui_AngoloDiRiconoscimento.Value = e.Angle;
        }
        private void AudioSourceSoundSourceAngleChanged(object sender, SoundSourceAngleChangedEventArgs e)
        {
            this.ui_SorgenteSonora.Value = e.Angle;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.sensore != null)
            {
                this.sensore.AudioSource.Stop();
                this.sensore.Stop();
            }
        }
    }
}
